/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cuong.bean;

import client.entity.Account;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.EncryptionPassword;
import util.HibernateUtil;

/**
 *
 * @author Admin Windows 8
 */
@ManagedBean
@SessionScoped
public class LoginBean {

    /**
     * Creates a new instance of LoginBean
     */
    public LoginBean() {
    }
    private String status = "fail";
    private String username;
    private String password;
    private boolean rememberMe;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isRememberMe() {
        return rememberMe;
    }

    public void setRememberMe(boolean rememberMe) {
        this.rememberMe = rememberMe;
    }

    public String login() {
        Account account = null;
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        account = (Account) ss.createQuery("from Account ac where ac.useName =:username").setParameter("username", username).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        if (account != null) {
            String pass = new EncryptionPassword().Decryption(account.getPassword());
            if (account.getUseName().equals(username) && pass.equals(password)) {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("account", account);
                status = "true";
                if (rememberMe) {
                    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
                    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
                    //Save to cookie
                    Cookie uName = new Cookie("username_w2w", username);
                    uName.setMaxAge(60 * 60 * 24 * 365); // Make the cookie last a year!
                    response.addCookie(uName);
                    //Save to cookie
                    Cookie pWord = new Cookie("password_w2w", password);
                    pWord.setMaxAge(60 * 60 * 24 * 365); // Make the cookie last a year!
                    response.addCookie(pWord);
                }
                if (account.getRole() == true) {
                    return "employment.xhtml?faces-redirect=true";
                } else {
                    return "worker.xhtml?faces-redirect=true";
                }

            }
        }
        return null;
    }

    public String goPage() {
        HttpSession ss = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        ss.removeAttribute("customerProfile");
        ss.removeAttribute("workerProfile");
        Account ac = (Account) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("account");
        if (ac.getRole() == true) {
            return "customerProfile.xhtml?faces-redirect=true";
        } else {
            return "workerProfile.xhtml?faces-redirect=true";
        }
    }

    public void loginListener() {
        if (status.equals("fail")) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Username or Password incorrect", "Please Try Again");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public String logout() {
//        Account ac = (Account) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("account");
//        if (ac != null) {          
//            HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
//                    .getExternalContext().getSession(true);
//            session.removeAttribute("account");  
//        }
        ((HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true)).invalidate();
        this.username = null;
        this.password = null;
        return "login.xhtml";
    }
}
